"""
Case Type   : HTAP行列融合特性
Case Name   : enable_parallel_populate参数功能验证(指定分区转换)
Create At   : 2025/1/23
Owner       : ningyali
Description :
    1.建表插入数据
    2.设置enable_parallel_populate为on，进行指定分区转换，获取time1
    3.设置enable_parallel_populate为off，进行指定分区转换，获取time2
    4.清理数据环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功，time2>time1
    4.执行成功
History     :
"""

import os
import unittest
from datetime import datetime

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.Constant = Constant()
        self.t_name = 't_htap_0125'
        self.show_sql = 'show enable_parallel_populate;'
        self.alter_sql = f'alter table {self.t_name} modify partition ' \
                         f'{self.t_name}_p4 imcstored;'

    def test_trans(self):
        text = '----step1:建表插入数据;expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name};' \
              f'create table {self.t_name}(' \
              f'    c1 integer(20),' \
              f'    c2 varchar(20) not null default \'\',' \
              f'    c3 char(30))' \
              f'partition by range (c1)(' \
              f'    partition {self.t_name}_p1 values less than (500000),' \
              f'    partition {self.t_name}_p2 values less than (1000000),' \
              f'    partition {self.t_name}_p3 values less than (3000000),' \
              f'    partition {self.t_name}_p4 values less than (maxvalue));' \
              f'insert into {self.t_name} values (' \
              f'generate_series(1, 5000000),' \
              f'generate_series(1, 5000000)||\'aaa\',' \
              f'generate_series(1, 5000000)||\'bbb\');'
        sql_res = self.pri_sh.execut_db_sql(sql)
        expect_res = r'CREATE TABLE\s+INSERT 0 5000000'
        self.assertRegex(sql_res, expect_res, f'建表插入数据失败:{text}')

        text = '----step2:设置enable_parallel_populate为on，进行指定分区转换，获取time1;' \
               'expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'reload', self.Constant.GSGUC_SUCCESS_MSG,
            f'enable_parallel_populate=on')
        self.assertEqual(True, guc_res, f'参数配置失败:{text}')

        show_res = self.pri_sh.execut_db_sql(self.show_sql)
        start_time = datetime.now()
        alter_res = self.pri_sh.execut_db_sql(self.alter_sql)
        end_time = datetime.now()
        run_time1 = (end_time - start_time).total_seconds() * 1000
        self.log.info(f'开启参数，start_time：{start_time}，end_time：{end_time}，'
                      f'指定分区转换耗时 {run_time1} ms')
        self.assertIn('on', show_res, f'执行失败:{text}')
        self.assertEqual('ALTER TABLE', alter_res.strip(), f'执行失败:{text}')

        text = '----step3:设置enable_parallel_populate为off，进行指定分区转换，获取time2;' \
               'expect:执行成功，time2>time1----'
        self.log.info(text)
        sql = f'alter table {self.t_name} modify partition ' \
              f'{self.t_name}_p4 unimcstored;'
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.assertEqual('ALTER TABLE', sql_res.strip(), f'执行失败:{text}')

        guc_res = self.pri_sh.execute_gsguc(
            'reload', self.Constant.GSGUC_SUCCESS_MSG,
            f'enable_parallel_populate=off')
        self.assertEqual(True, guc_res, f'参数配置失败:{text}')

        show_res = self.pri_sh.execut_db_sql(self.show_sql)
        start_time = datetime.now()
        alter_res = self.pri_sh.execut_db_sql(self.alter_sql)
        end_time = datetime.now()
        run_time2 = (end_time - start_time).total_seconds() * 1000
        self.log.info(f'开启参数，start_time：{start_time}，end_time：{end_time}，'
                      f'指定分区转换耗时 {run_time2} ms')
        self.assertIn('off', show_res, f'执行失败:{text}')
        self.assertEqual('ALTER TABLE', alter_res.strip(), f'执行失败:{text}')

        diff_time = run_time2 - run_time1
        self.log.info(f'开启enable_parallel_populate后，行列转换后耗时减少{diff_time}ms')
        self.assertGreater(diff_time, 0,
                           f'开启enable_parallel_populate后，行列转换后耗时未减少')

    def tearDown(self):
        text = '----step4:清理环境;expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'reload', self.Constant.GSGUC_SUCCESS_MSG,
            f'enable_parallel_populate')

        sql = f'drop table if exists {self.t_name};'
        sql_res = self.pri_sh.execut_db_sql(sql)

        self.assertEqual(True, guc_res, f'参数配置失败:{text}')
        self.assertIn('DROP TABLE', sql_res, f'删除表失败:{text}')

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
